import QQMapWX from '@/common/qqmap-wx-jssdk.js';
import Vue from 'vue'
import {
	mapState,
	mapMutations
} from 'vuex';

class Location {
	constructor(arg) {

	}

	// ...mapMutations(['SET_LOCATION']),
	wxgetLocation(e) {
		return new Promise((resolve, reject) => {
			uni.authorize({
				scope: "scope.userLocation",
				success: (res) => {

					let qqmapsdk = new QQMapWX({
						// 填写自己的Key值，这个值是与AppID绑定的
						key: 'Y22BZ-HHIC2-MVOU4-CH2HX-YVBG7-ZEFJB'
					});
					uni.getLocation({
						type: 'gcj02',
						success: (res) => {
							qqmapsdk.reverseGeocoder({
								location: {
									latitude: res.latitude,
									longitude: res.longitude
								},
								success: (res) => {
									res.name = res.result.ad_info
										.district;
									res.citycode = res.result.ad_info
										.adcode;
									res.province = res.result.ad_info
										.province;
									res.city = res.result.ad_info.city;
									// this.myCityObj = res;
									// this.hasLocation = true;
									// this.SET_LOCATION(res)
									resolve(res)
								},
								fail: (err) => {
									console.log(err)
									reject(err)
									uni.showToast({
										icon: 'none',
										title: '获取用户定位失败，请手动选择当前城市'
									})
								}
							})
						}
					})
				},
				fail: (err) => {
					console.log(err)
					uni.showModal({
						content: '检测到您还没有打开位置授权，是否去设置打开？',
						confirmText: "确认",
						cancelText: "取消",
						success: function(res) {
							if (res.confirm) {
								uni.openSetting({
									success: (res) => {

									}
								})
							} else {
								console.log('用户点击取消')
							}
						}
					})
				}
			})
		})


	}

	H5getLocation() {
		return new Promise((resolve, reject) => {
			console.log(11)
			uni.getLocation({
				type: 'gcj02', //默认为 wgs84 返回 gps 坐标，gcj02 返回国测局坐标
				success: (res) => {
					console.log(res)
					let url = 'https://apis.map.qq.com/ws/geocoder/v1/'
					let data = {
						location: res.latitude + ',' + res.longitude,
						key: 'YXOBZ-GDMLJ-Z2OFQ-FBDNS-6BP7V-FCBYD',
						output: 'jsonp',
						callback: 'jsonp'
					}
					Vue.prototype.$jsonp(url, data).then(res => {
						console.log(res, "res1")
						if (res.status == 0) {
							res.name = res.result.ad_info.district;
							res.citycode = res.result.ad_info.adcode;
							res.province = res.result.ad_info.province;
							res.city = res.result.ad_info.city;
							// this.myCityObj = res;
							// this.hasLocation = true;
							// this.SET_LOCATION(res)
							resolve(res)
						}
					}).catch(err => {
						console.log(err, "err1")
					})
				},
				fail:(err)=> {
					console.log(err, "err2")
				}
			})
		})


		console.log("H5getLocation")
	}

}

export default Location;
